document.addEventListener('DOMContentLoaded', () => {
    // 游戏状态
    const gameState = {
        currentPlayer: 'X',
        board: Array(9).fill(''),
        gameActive: true,
        movesCount: 0
    };

    // 获取DOM元素
    const board = document.getElementById('board');
    const status = document.getElementById('status');
    const restartBtn = document.getElementById('restart');
    const cells = document.querySelectorAll('.cell');

    // 胜利条件
    const winningConditions = [
        [0, 1, 2], // 第一行
        [3, 4, 5], // 第二行
        [6, 7, 8], // 第三行
        [0, 3, 6], // 第一列
        [1, 4, 7], // 第二列
        [2, 5, 8], // 第三列
        [0, 4, 8], // 主对角线
        [2, 4, 6]  // 副对角线
    ];

    // 初始化游戏
    function initGame() {
        gameState.currentPlayer = 'X';
        gameState.board = Array(9).fill('');
        gameState.gameActive = true;
        gameState.movesCount = 0;
        status.textContent = `轮到 ${gameState.currentPlayer} 方`;
        
        cells.forEach(cell => {
            cell.textContent = '';
            cell.classList.remove('x', 'o', 'winning');
        });
    }

    // 处理单元格点击
    function handleCellClick(e) {
        const cell = e.target;
        const index = parseInt(cell.getAttribute('data-index'));
        
        // 如果单元格已被占用或游戏结束，则返回
        if (gameState.board[index] !== '' || !gameState.gameActive) {
            return;
        }
        
        // 更新游戏状态
        gameState.board[index] = gameState.currentPlayer;
        cell.textContent = gameState.currentPlayer;
        cell.classList.add(gameState.currentPlayer.toLowerCase());
        gameState.movesCount++;
        
        // 检查是否胜利
        if (checkWin()) {
            status.textContent = `${gameState.currentPlayer} 方胜利!`;
            gameState.gameActive = false;
            return;
        }
        
        // 检查是否平局
        if (gameState.movesCount === 9) {
            status.textContent = '平局!';
            gameState.gameActive = false;
            return;
        }
        
        // 切换玩家
        gameState.currentPlayer = gameState.currentPlayer === 'X' ? 'O' : 'X';
        status.textContent = `轮到 ${gameState.currentPlayer} 方`;
    }

    // 检查胜利条件
    function checkWin() {
        return winningConditions.some(condition => {
            if (
                gameState.board[condition[0]] !== '' &&
                gameState.board[condition[0]] === gameState.board[condition[1]] &&
                gameState.board[condition[1]] === gameState.board[condition[2]]
            ) {
                highlightWinningCells(condition);
                return true;
            }
            return false;
        });
    }

    // 高亮显示获胜单元格
    function highlightWinningCells(condition) {
        condition.forEach(index => {
            cells[index].classList.add('winning');
        });
    }

    // 绑定事件监听器
    cells.forEach(cell => {
        cell.addEventListener('click', handleCellClick);
    });
    
    restartBtn.addEventListener('click', initGame);

    // 初始化游戏
    initGame();
}); 